package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.admin.mapper.AttrKeyMapper;
import com.atguigu.lease.web.admin.mapper.GraphInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.lease.web.admin.mapper.RoomInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
* @author morucheng
* @description 针对表【room_info(房间信息表)】的数据库操作Service实现
* @createDate 2024-10-30 15:12:52
*/
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
    implements RoomInfoService{

    @Autowired
    RoomInfoMapper roomInfoMapper;

    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    GraphInfoService graphInfoService;
    @Autowired
    RoomAttrValueService roomAttrValueService;
    @Autowired
    RoomFacilityService roomFacilityService;
    @Autowired
    RoomLabelService roomLabelService;
    @Autowired
    RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    RoomLeaseTermService roomLeaseTermService;

    @Autowired
    AttrValueService attrValueService;
    @Autowired
    AttrKeyMapper attrKeyMapper;

    @Autowired
    FacilityInfoService facilityInfoService;
    @Autowired
    LabelInfoService labelInfoService;
    @Autowired
    PaymentTypeService paymentTypeService;
    @Autowired
    LeaseTermService leaseTermService;


    @Override
    public IPage<RoomItemVo> pageItem(Page<RoomItemVo> roomItemVoPage, RoomQueryVo queryVo) {

        return roomInfoMapper.pageItem(roomItemVoPage,queryVo);
    }

    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        boolean isUpdate = roomSubmitVo.getId()!=null;
        super.saveOrUpdate(roomSubmitVo);
        Long roomId = roomSubmitVo.getId();

        if(isUpdate){

            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType, ItemType.ROOM).eq(GraphInfo::getItemId,roomId));
            roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId,roomId));
            roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId,roomId));
            roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId,roomId));
            roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId,roomId));
            roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId,roomId));

        }

        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphlist = graphVoList.stream().map(graphVo ->
            {
                GraphInfo graphInfo = new GraphInfo();
                BeanUtils.copyProperties(graphVo, graphInfo);
                graphInfo.setItemId(roomId);
                graphInfo.setItemType(ItemType.ROOM);
                return graphInfo;
            }).toList();
            graphInfoService.saveBatch(graphlist);
        }

        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)){
            roomFacilityService.saveBatch(facilityInfoIds.stream().map(facilityInfoId->
                    RoomFacility.builder()
                    .roomId(roomId)
                    .facilityId(facilityInfoId)
                    .build()).toList());
        }

        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if(!CollectionUtils.isEmpty(labelInfoIds)){
            roomLabelService.saveBatch(labelInfoIds.stream().map(labelInfoId->RoomLabel.builder().roomId(roomId).labelId(labelInfoId).build()).toList());
        }

        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if(!CollectionUtils.isEmpty(attrValueIds)){
            roomAttrValueService.saveBatch(attrValueIds.stream().map(attrValueId->RoomAttrValue.builder().attrValueId(attrValueId).roomId(roomId).build()).toList());
        }

        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if(!CollectionUtils.isEmpty(paymentTypeIds)){
            roomPaymentTypeService.saveBatch(paymentTypeIds.stream().map(paymentTypeId->RoomPaymentType.builder().paymentTypeId(paymentTypeId).roomId(roomId).build()).toList());
        }

        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if(!CollectionUtils.isEmpty(leaseTermIds)){
            roomLeaseTermService.saveBatch(leaseTermIds.stream().map(leaseTermId->RoomLeaseTerm.builder().leaseTermId(leaseTermId).roomId(roomId).build()).toList());
        }



    }

    @Override
    public RoomDetailVo getRoomById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfoMapper.selectById(id),roomDetailVo);

        roomDetailVo.setApartmentInfo(apartmentInfoMapper.selectById(roomDetailVo.getApartmentId()));

        List<GraphInfo> graphInfolist = graphInfoService.list(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType, ItemType.ROOM).eq(GraphInfo::getItemId, id));
        List<GraphVo> graphVoList = graphInfolist.stream().map(graphInfo -> {
            GraphVo graphVo = new GraphVo();
            BeanUtils.copyProperties(graphInfo, graphVo);
            return graphVo;
        }).toList();
        roomDetailVo.setGraphVoList(graphVoList);

        List<RoomAttrValue> roomAttrValuelist = roomAttrValueService.list(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, id));
        List<AttrValueVo> attrValueVoList = roomAttrValuelist.stream().map(roomAttrValue -> {
            AttrValue attrValue = attrValueService.getById(roomAttrValue.getAttrValueId());
            AttrValueVo attrValueVo = new AttrValueVo();
            BeanUtils.copyProperties(attrValue, attrValueVo);
            String keyName = attrKeyMapper.selectById(attrValue.getAttrKeyId()).getName();
            attrValueVo.setAttrKeyName(keyName);
            return attrValueVo;
        }).toList();
        roomDetailVo.setAttrValueVoList(attrValueVoList);

        List<RoomFacility> roomFacilityList = roomFacilityService.list(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, id));
        List<FacilityInfo> facilityInfoList = roomFacilityList.stream().map(roomFacility -> facilityInfoService.getById(roomFacility.getFacilityId())).toList();
        roomDetailVo.setFacilityInfoList(facilityInfoList);

        List<RoomLabel> roomLabelList = roomLabelService.list(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, id));
        List<LabelInfo> labelInfoList = roomLabelList.stream().map(roomLabel -> labelInfoService.getById(roomLabel.getLabelId())).toList();
        roomDetailVo.setLabelInfoList(labelInfoList);

        List<RoomPaymentType> roomPaymentTypeList = roomPaymentTypeService.list(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, id));
        List<PaymentType> paymentTypeList = roomPaymentTypeList.stream().map(roomPaymentType -> paymentTypeService.getById(roomPaymentType.getPaymentTypeId())).toList();
        roomDetailVo.setPaymentTypeList(paymentTypeList);

        List<LeaseTerm> leaseTermList = roomLeaseTermService.list(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId,id))
                .stream().map(roomLeaseTerm -> leaseTermService.getById(roomLeaseTerm.getLeaseTermId())).toList();
        roomDetailVo.setLeaseTermList(leaseTermList);


        return roomDetailVo;
    }

    @Override
    public void removeRoomById(Long id) {
        roomInfoMapper.deleteById(id);
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType, ItemType.ROOM).eq(GraphInfo::getItemId,id));
        roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId,id));
        roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId,id));
        roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId,id));
        roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId,id));
        roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId,id));
    }

    @Override
    public void updateReleaseStatusById(Long id, ReleaseStatus status) {
        super.update(new LambdaUpdateWrapper<RoomInfo>().eq(RoomInfo::getId,id).set(RoomInfo::getIsRelease,status));
    }

    @Override
    public List<RoomInfo> listBasicByApartmentId(Long id) {

        return super.list(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId,id));
    }
}




